Skip to main content

Sample

df.sample(n, replace=False)

Randomly samples n rows from df.

Input:
df : DataFrame
DataFrame to sample from.
n : integer
Number of rows to draw.
replace : boolean, default False
If True, rows are selected with replacement. If False, rows are selected without replacement.
Returns:
A random sample of n rows from df
Return Type:
DataFrame
Note:
  • If replace = False, n must be smaller than the length of the DataFrame. Otherwise, the function will raise a ValueError: Cannot take a larger sample than length of DataFrame when 'replace=False'.

pets.sample(3, replace=True)
IndexIDUnnamed: 0SpeciesColorWeightAgeIs_CatOwner_Comment
4dog_0034dogblack250.5FalseBe the person your dog thinks you are.
6ham_0026hamstergolden0.250.2FalseNo, thank you!
1cat_0011catgolden1.50.2TrueMy best birthday present ever!!!
pets.sample(3, replace=False)
IndexIDUnnamed: 0SpeciesColorWeightAgeIs_CatOwner_Comment
1cat_0011catgolden1.50.2TrueMy best birthday present ever!!!
6ham_0026hamstergolden0.250.2FalseNo, thank you!
4dog_0034dogblack250.5FalseBe the person your dog thinks you are.
pets.shape[0]

8

pets.sample(9, replace=False)

ValueError: Cannot take a larger sample than length of DataFrame when 'replace=False'